﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Reflection;

namespace Macaw.Moss2007.CustomErrorPageSolution
{
    /// <summary>
    /// http://www.codeproject.com/KB/sharepoint/Handling_Error_Centrally.aspx
    /// </summary>
    public class CustomErrorPageModule : IHttpModule
    {


        #region IHttpModule Members

        void IHttpModule.Dispose()
        {

        }

        void IHttpModule.Init(HttpApplication context)
        {
            System.Diagnostics.Debug.WriteLine("CustomErrorPageModule: Init");
            context.Error += new EventHandler(Application_Error);
        }


        void CustomErrorPageModule_Error(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("CustomErrorPageModule: Page Error");
        }


        #endregion


        private const string CUSTOMERRORPAGE = "CustomErrorPage";

        public void Application_Error(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("CustomErrorPageModule: Application Error");

            HttpContext Context = HttpContext.Current;
            
            Exception exception = Context.Server.GetLastError();

            String code = DateTime.Now.ToString("ddMMyyyy-HHmmfff");
            String Message = exception.Message + Environment.NewLine +
                             "Stack Trace: " + exception.StackTrace;
            System.Diagnostics.Debug.WriteLine(Message);

            if (exception.InnerException != null)
            {
                System.Diagnostics.Debug.WriteLine(exception.InnerException.ToString());
            }

            string customerrorpageurl = CustomErrorPageHelper.GetCustomErrorPageUrl(SPContext.Current.Site.ID, SPContext.Current.Web.ID, true);
            if (!string.IsNullOrEmpty(customerrorpageurl))
            {
                Context.ClearError();
                Context.Response.Clear();
                Context.Server.Transfer(customerrorpageurl);
                return;
            }
            
            

                      //var factory = new CEPSPageHandlerFactory();
            //IHttpHandler handler = factory.GetHandler(null, null, "~/_layouts/Macaw.Moss2007.CustomErrorPageSolution/GenericCustomErrorPage.aspx" , null);            
            //Context.Handler = handler;            
            //handler.ProcessRequest(Context);

            /*
            string url = "http://cms.hema.local/Pages/foutpagina.aspx";
            using (SPSite site = new SPSite(url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPListItem item = web.GetListItem(url);
                    HttpContext.Current.Items["DefaultSPContext"] =  SPContext.GetContext(Context, item.ID, item.ParentList.ID , web);
                    HttpContext.Current.Items["Microsoft.SharePoint.Administrtion.ContextUri" ] = new Uri(url);
                    HttpContext.Current.Items["WSS_PageUrlPath"] = url;
                    HttpContext.Current.Items["HttpHandlerSPWeb"] = web;

                    var factory = new CEPSPageHandlerFactory();
                    IHttpHandler handler = factory.GetHandler(null, null, "~/_layouts/GenericCustomErrorPage.aspx", null);
                    Context.Handler = handler;
                    try
                    {
                        handler.ProcessRequest(Context);
                    }
                    catch
                    {
                        //
                    }
                }
            }*/
            
            



            /*if (SPContext.Current != null && SPContext.Current.Site != null)
            {
                using (SPWeb web = SPContext.Current.Site.OpenWeb())
                {
                    if (web.Properties.ContainsKey(CUSTOMERRORPAGE))
                    {
                        string CustomErrorPageUrl = (string) web.Properties[CUSTOMERRORPAGE];
                        if (!string.IsNullOrEmpty(CustomErrorPageUrl))
                        {
                            CustomErrorPageUrl = string.Format(CustomErrorPageUrl, SPContext.Current.Web.Locale.Name);
                            CustomErrorPageUrl = "~/_layouts/viewlsts.aspx";
                            Context.ClearError();

                            var factory = new CEPSPageHandlerFactory();
                            IHttpHandler handler = factory.GetHandler(null, null, CustomErrorPageUrl , null);
                            handler.ProcessRequest(Context);
                        }
                    }
                }
            }*/

        }
    }

}
